import pika
# auth=pika.PlainCredentials('root','root') #save auth indo
# connection = pika.BlockingConnection(pika.ConnectionParameters(
#   '127.0.0.1',5672,'/',auth)) #connect to rabbit
# channel = connection.channel() #create channel
# channel.queue_declare(queue='queque',durable=True) #declare queue
# #n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
# channel.basic_publish(exchange='',
#    routing_key='queque',
#    body='Hello World!') #the body is the msg content
# print(" [x] Sent 'Hello World!'")
# connection.close()


class MQ():
    def __init__(self, name_que, auth_name, auth_pwd, address_dateway, address_port, virtual_host,auto_ack = True, exchange_type = 'fanout', durable = True):
        self.name_que = name_que
        self.auth_name = auth_name
        self.auth_pwd = auth_pwd
        self.address_dataway = address_dateway
        self.address_port = address_port
        self.virtual_host = virtual_host
        self.auto_ack = auto_ack
        self.exchange_type = exchange_type
        self.durable = durable


    def create_MQ(self):
        '''
        创建通道
        :param auth_name: auth 账号
        :param auth_pwd: auth 密码
        :param address_dateway: 网关地址
        :param address_port: 端口
        :return: connection
        '''
        auth = pika.PlainCredentials(self.auth_name, self.auth_pwd)
        address_dateway = self.address_dataway
        address_port = self.address_port
        virtual_host = self.virtual_host
        connection = pika.BlockingConnection(pika.ConnectionParameters(
            address_dateway, address_port, virtual_host, auth, heartbeat=0,
        ))
        return connection

    def send_Message_MQ(self, connection, message):
        '''
        发送信息
        :param connection: 通道create_MQ返回
        :param message: 要发送的信息
        :param durable: true  or  false
        :return:
        '''
        channel = connection.channel()
        #create queue
        channel.queue_declare(queue=self.name_que, durable=self.durable)

        #create exchange
        channel.exchange_declare(exchange= self.name_que, exchange_type = self.exchange_type, durable = self.durable)

        # publish to queue
        # channel.basic_publish(
        #     exchange = '',
        #     routing_key = self.name_que,
        #     body = message
        # )

        #publish to exchange
        channel.basic_publish(
            exchange = '',
            routing_key=self.name_que,
            body=message

        )
        connection.close()



    def send_heart_Message_MQ(self, connection, message):
        '''
        发送信息
        :param connection: 通道create_MQ返回
        :param message: 要发送的信息
        :param durable: true  or  false
        :return:
        '''
        channel = connection.channel()
        # create queue
        channel.queue_declare(queue=self.name_que, durable=self.durable)

        # create exchange
        channel.exchange_declare(exchange=self.name_que, exchange_type=self.exchange_type, durable=self.durable)

        # publish to queue
        # channel.basic_publish(
        #     exchange = '',
        #     routing_key = self.name_que,
        #     body = message
        # )

        # publish to exchange
        channel.basic_publish(
            exchange='',
            routing_key=self.name_que,
            body=message

        )
        # connection.close()



    def callback(ch, body):
        '''
        get_Message_MQ:on_message_callback
        - channel: BlockingChannel
             - method: spec.Basic.Deliver
             - properties: spec.BasicProperties
             - body: bytes
        :param method:
        :param properties:
        :param body: message
        :return:
        '''
        print(" [x] Received %r" % body)
        # return body

    def get_Message_MQ(self, connection, allback = callback):
        channel = connection.channel()
        channel.queue_declare(queue=self.name_que, durable=self.durable)
        message = channel.basic_consume(
            queue=self.name_que, on_message_callback=allback, auto_ack=self.auto_ack)
        channel.start_consuming()
        return message

    def close_MQ(self, connection):
        '''

        :param connection: 通道create_MQ返回
        :return:
        '''
        connection.close()